package com.wn26.dao.impl;

import java.sql.SQLException;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import com.wn26.bean.User;
import com.wn26.dao.UserDao;
import com.wn26.utils.DataSourceUtils;

public class UserDaoImpl implements UserDao {

	@Override
	public User findUserByUserNameAndPwd(String userName, String userPwd) throws SQLException {
		QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
		// -- 查询用query这个方法
		return runner.query("select * from _user where user_name = ? and user_pwd = ?",
				new BeanHandler<User>(User.class), userName, userPwd);
	}

	@Override
	public int saveUser(User registerUser) throws SQLException {
		QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
		return runner.update("insert into _user values(?,?,?,?)", registerUser.getUser_id(),
				registerUser.getUser_name(), registerUser.getUser_pwd(), registerUser.getUser_tel());
	}

	@Override
	public User findUserByTel(String user_tel) throws SQLException {
		QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
		return runner.query("select * from _user where user_tel = ?", new BeanHandler<User>(User.class), user_tel);
	}

}
